package com.shuke.medical.insure.service;

import cn.hutool.core.net.NetUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.shuke.medical.insure.config.ChsProperties;
import com.shuke.medical.insure.eums.ChsInfNoEnum;
import com.shuke.medical.insure.model.bo.ChsMdRegister;
import com.shuke.medical.insure.model.bo.ChsMdVisitUpload;
import com.shuke.medical.insure.model.bo.ChsSignOutBO;
import com.shuke.medical.insure.remote.ChsRemoteHttp;
import com.shuke.medical.insure.remote.consts.ChsCacheConst;
import com.shuke.medical.insure.utils.ChsInOutUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.InetAddress;

/***
 * @title ChsMdService
 * @description: 门诊业务管理
 * @author woo hua
 * @version 1.0.0
 * @create 2023/4/3 17:11
 **/
@Slf4j
@Service
public class ChsMdService {

    @Resource
    private ChsRemoteHttp chsRemoteHttp;
    @Resource
    private ChsProperties chsProperties;
    @Resource
    private RedisTemplate redisTemplate;

    public ChsMdRegister.Output do2201(ChsMdRegister chsMdRegister) throws Exception {
        JSONObject input = new JSONObject();
        input.put(ChsMdRegister.MD_TRT_INFO, chsMdRegister.getMdTrtInfo());
        input.put(ChsMdRegister.AGNTER_INFO, chsMdRegister.getAgnterInfo());
        try {
            String result = chsRemoteHttp.doPost(ChsInfNoEnum.CHS_INF_2201, input);
            ChsMdRegister.Output output = ChsInOutUtil.toOutPutByKey(result, ChsInOutUtil.DEFAULT_INPUT_KEY, ChsMdRegister.Output.class);
            return output;
        } catch (Exception e) {
            log.error("问诊挂号登记失败， cause: [{}]", e.getMessage());
            throw new Exception("问诊挂号登记失败:" + e.getMessage());
        }
    }

    public void do2203(ChsMdVisitUpload visitUpload) throws Exception {
        JSONObject input = new JSONObject();
        input.put(ChsMdVisitUpload.MD_TRT_INFO, visitUpload.getMdTrtInfo());
        input.put(ChsMdVisitUpload.DISE_INFO, visitUpload.getDiseInfo());
        try {
            String result = chsRemoteHttp.doPost(ChsInfNoEnum.CHS_INF_2203, input);
            log.info("门诊就诊信息上传结果: [{}]", result);
        } catch (Exception e) {
            log.error("门诊就诊信息上传失败， cause: [{}]", e.getMessage());
            throw new Exception("门诊就诊信息上传失败:" + e.getMessage());
        }
    }

    public void do9002(ChsSignOutBO.Input input) throws Exception {
        InetAddress localHost = NetUtil.getLocalhost();
        String ip = StringUtils.isEmpty(chsProperties.getDynamicConfig().getIp()) ? localHost.getHostAddress() : chsProperties.getDynamicConfig().getIp();
        String mac = StringUtils.isEmpty(chsProperties.getDynamicConfig().getMac()) ? NetUtil.getMacAddress(localHost) : chsProperties.getDynamicConfig().getMac();
        input.setMac(mac);
        input.setIp(ip);
        input.setOpter_no(chsProperties.getAccount().getOpterNo());
        if (StringUtils.isEmpty(input.getSign_no())) {
            Object signNo = redisTemplate.opsForValue().get(ChsCacheConst.CHS_SIGN_NO);
            if (null != signNo) {
                input.setSign_no(signNo.toString());
            }
        }
        String result = chsRemoteHttp.doPost(ChsInfNoEnum.SIGN_OUT, ChsInOutUtil.toInPut(input));
        log.info("退出结果： [{}]", result);
    }
}
